package com.lauor.test.delete;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lauor.smpedr.param.SqlArgMap;
import com.lauor.smpedr.session.SqlSession;
import com.lauor.test.BaseSpringBootUser;
import com.lauor.test.entity.User;
import org.junit.Test;

/**
 * 正常删除
 */
public class NormalDeleteUser extends BaseSpringBootUser {

    @Test
    public void testDelete(){
        User user = this.buildUserMsg();
        try {
            SqlSession sqlSession = sqlSessionFactory.openSession();
            //创建一条
            int rows = sqlSession.insert(User.class, user);
            System.out.println( String.format("创建%d条", rows) );
            //创建后查询
            SqlArgMap<Integer> sqlArgMap = new SqlArgMap<>(4);
            sqlArgMap.put("id", user.getId());
            user = sqlSession.selectOne(User.class, sqlArgMap);
            System.out.println( String.format("新创建数据:%s", JSON.toJSONString(user, SerializerFeature.WriteMapNullValue)) );

            //删除
            rows = sqlSession.delete(User.class, sqlArgMap);
            System.out.println( String.format("删除%d条", rows) );
            //再查询
            user = sqlSession.selectOne(User.class, sqlArgMap);
            System.out.println( String.format("删除后数据:%s", JSON.toJSONString(user, SerializerFeature.WriteMapNullValue)) );
        } catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
